package com.example.ycdyus.dailyfruit.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.ycdyus.dailyfruit.BaseApp;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.adapter.CategoryGridAdapter;
import com.example.ycdyus.dailyfruit.adapter.DemoAdapter;
import com.example.ycdyus.dailyfruit.bean.CategoryBean;
import com.example.ycdyus.dailyfruit.bean.Common;
import com.example.ycdyus.dailyfruit.bean.Sublevel;
import com.example.ycdyus.dailyfruit.constant.URLConstant;
import com.example.ycdyus.dailyfruit.request.FastJsonRequest;
import com.example.ycdyus.dailyfruit.utils.Utils;
import com.example.ycdyus.dailyfruit.utils.VolleyHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryFragment extends Fragment {
    private ListView mianCategoryLeftListview;
    private TextView mainCategoryRightTitle;
    private GridView mainCategoryRightGridview;
    private List<String> listView = new ArrayList<>();
    private ArrayAdapter<String> listAdapter;
    private List<CategoryBean> listData;
    private List<Common> bean;
    private List<Sublevel> listRightGrid = new ArrayList<>();
    private CategoryGridAdapter categoryGridAdapter;


    public CategoryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View  view = inflater.inflate(R.layout.fragment_category, container, false);
        initView(view);
        initData();
        return view;
    }

    private Map<String, String> getParam(){
        Map<String,String> map =  new HashMap<>();
        map.put("sign","c316fe3aee275e34712eea2cdaa92d70");
        map.put("timestamp","1438873421");
        map.put("platform","ANDROID");
        map.put("source","app");
        map.put("connect_id","");
        map.put("region_id","143949");
        map.put("service","product.getCatList");
        map.put("device_id","93789e8978f2eefdcf52006b4d96ee23");
        map.put("channel", "qq");
        map.put("version", "2.3.0");
        return map;
    }

    private void initData() {
        listAdapter = new ArrayAdapter<String>(getActivity(),R.layout.item_category_listview_left,R.id.tv_item_category_listview,listView);
        mianCategoryLeftListview.setAdapter(listAdapter);

        categoryGridAdapter = new CategoryGridAdapter(getActivity(),listRightGrid,R.layout.item_category_right_grid);
        mainCategoryRightGridview.setAdapter(categoryGridAdapter);

        FastJsonRequest<CategoryBean> request = new FastJsonRequest<CategoryBean>(Request.Method.POST, URLConstant.MAIN_CATEGORY_POST, getParam(), CategoryBean.class, new Response.Listener<CategoryBean>() {
            @Override
            public void onResponse(CategoryBean response) {
                if(response!=null){
                    bean = response.getCommon();
                    if(bean !=null){
                        for (int i = 0; i < response.getCommon().size(); i++) {
                            listView.add(bean.get(i).getName());
                        }
                        listAdapter.notifyDataSetChanged();
                        BaseApp.showToast(""+mianCategoryLeftListview.getChildCount());
//                        mianCategoryLeftListview.getChildAt(fetch).setBackgroundColor(Color.WHITE);

                        mainCategoryRightTitle.setText(bean.get(0).getName());
                        if(listRightGrid!=null){
                            if(!listRightGrid.isEmpty()){
                                listRightGrid.clear();
                            }
                            listRightGrid.addAll(bean.get(0).getSub_level());
                            categoryGridAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleyHelper.getInstance().addRequest(request,this);
    }

    /**
     * 初始化界面
     * @param view
     */
    private void initView(View view) {
        mianCategoryLeftListview = (ListView) view.findViewById(R.id.mian_category_left_listview);
        mainCategoryRightTitle = (TextView) view.findViewById(R.id.main_category_right_title);
        mainCategoryRightGridview = (GridView) view.findViewById(R.id.main_category_right_gridview);

        mianCategoryLeftListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < listView.size(); i++) {
                    parent.getChildAt(i).setBackgroundColor(Color.rgb(238,238,238));
                }
                view.setBackgroundColor(Color.WHITE);

                mainCategoryRightTitle.setText(bean.get(position).getName());

                /**
                 * 给右边的GridView进行数据加载
                 */
                if(listRightGrid!=null){
                    if(!listRightGrid.isEmpty()){
                        listRightGrid.clear();
                    }
                    listRightGrid.addAll(bean.get(position).getSub_level());
                    categoryGridAdapter.notifyDataSetChanged();
                }

                BaseApp.showToast(""+mianCategoryLeftListview.getChildCount());
            }
        });
    }


}
